//
//  MainJumpViewController.m
//  Item A House
//
//  Created by dlios on 15/5/25.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MainJumpViewController.h"
#import "JumpHeadView.h"
#import "SANetWorkingTask.h"
#import "DynamicTableViewCell.h"
#import "DynamicJumpViewController.h"
#import "DetailMainViewController.h"
#import "LayoutTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "LayoutJumpViewController.h"
#import "NewCollectionModel.h"
#import "RankModel.h"

@interface MainJumpViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation MainJumpViewController
- (void)dealloc
{
    [_collectionButton release];
    [_loupan_name release];
    [_layoutArray release];
    [_dynamicArray release];
    [_rankModel release];
    [_loupan_id release];
    [_infomaArr release];
    [_table release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dynamicArray = [NSMutableArray array];
        self.layoutArray = [NSMutableArray array];
    }
    return self;
}
//动态网络请求
- (void)createDynamicNet
{
    NSDictionary *dic = @{@"loupan_ids":self.loupan_id};
    [SANetWorkingTask request:@"http://api.anjuke.com/xinfang/m/iphone/1.3/inform/feeds/recommend/" parmater:dic block:^(NSDictionary *result) {
        NSDictionary *dic1 = [result objectForKey:@"result"];
        self.dynamicArray = [dic1 objectForKey:@"rows"];
        [self.table reloadData];
    }];
//    if ([self.dynamicArray count] == 0) {
//        self.collectionButton.enabled = NO;
//        self.table.userInteractionEnabled = NO;
//    } else {
//        self.collectionButton.enabled = YES;
//        self.table.userInteractionEnabled = YES;
//    }
}
- (void)createLayout
{
   NSDictionary *dic = @{@"loupan_id":self.loupan_id};
    [SANetWorkingTask request:@"http://api.anjuke.com/xinfang/m/iphone/1.3/housetype/list/" parmater:dic block:^(NSDictionary *result) {
        self.layoutArray = [result objectForKey:@"result"];
        [self.table reloadData];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    
    [self createDynamicNet];
    [self createTable];
    [self createLayout];
    
    JumpHeadView *jump = [[JumpHeadView alloc]  initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width * 0.5 + 100)];
    [jump setid:self.loupan_id];
    [jump headViewNormal:self.rankModel];
   
    self.table.tableHeaderView = jump;
    
    self.collectionButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.collectionButton addTarget:self action:@selector(collectionButton:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.collectionButton];
    [self.collectionButton release];
    UIImage *image = [UIImage imageNamed:@"My.png"];
    self.collectionButton.enabled = NO;

    [_collectionButton setImage:image forState:UIControlStateNormal];
    [self.navigationItem setHidesBackButton:YES];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(15, 5, 38, 38);
    
    [btn setBackgroundImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    
    [btn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
    
    UIBarButtonItem*back=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem=back;
}

-(void)goBackAction
{
    [self.navigationController popViewControllerAnimated:YES];

//    [self.collectionButton setImage:image forState:UIControlStateNormal];

}

- (void)collectionButton:(id)sender
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:self.loupan_id forKey:@"loupan_id"];
    [dic setObject:self.loupan_name forKey:@"loupan_name"];

    [dic setObject:self.rankModel.region_title forKey:@"region_title"];
    [dic setObject:self.rankModel.sub_region_title forKey:@"sub_region_title"];
    [dic setObject:self.rankModel.tags forKey:@"tags"];
    NSString *Str = [NSString stringWithFormat:@"%ld", (long)self.rankModel.price];
    [dic setObject:Str forKey:@"price"];
    [dic setObject:self.rankModel.kaipan_new_date forKey:@"kaipan_new_date"];

    [NewCollectionModel writeArray:dic];
}

- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    //申请自动释放池
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table registerClass:[DynamicTableViewCell class] forCellReuseIdentifier:@"dynamic"];
    [self.table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.table registerClass:[LayoutTableViewCell class] forCellReuseIdentifier:@"layout"];
    [self.table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"mycell"];
    [self.view addSubview:self.table];
    [self.table release];
}
#pragma mark -
#pragma mark table的协议
//row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section == 0) {
        
        return 2;
    }
    if (section == 1) {
        return 2;
    }
    return 0;
}
//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
            DynamicTableViewCell *dynamic = [tableView dequeueReusableCellWithIdentifier:@"dynamic"];
            //点击不变色
            dynamic.selectionStyle = UITableViewCellSelectionStyleNone;
            //大于号
            dynamic.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            if (self.dynamicArray.count == 0) {
                
                return dynamic;
            } else {
                
                NSDictionary *myDic = [self.dynamicArray objectAtIndex:0];
                dynamic.titleLabel.text = [myDic objectForKey:@"title"];
                dynamic.inforLabel.text = [myDic objectForKey:@"summary"];
                self.collectionButton.enabled = YES;
                return dynamic;
            }
            
        }
        if (indexPath.row == 1) {
            
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = @"更多动态";
            cell.detailTextLabel.text = @">";
            return cell;
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            LayoutTableViewCell *layout = [tableView dequeueReusableCellWithIdentifier:@"layout"];
            //点击不变色
            layout.selectionStyle = UITableViewCellSelectionStyleNone;
            //大于号
            layout.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            if ([self.layoutArray count] == 0) {
                return layout;
            } else {
                NSDictionary *bigDic = [self.layoutArray objectAtIndex:0];
                NSMutableArray *row = [bigDic objectForKey:@"rows"];
                NSDictionary *smallDic = [row objectAtIndex:0];
                [layout.image sd_setImageWithURL:[NSURL URLWithString:[smallDic objectForKey:@"default_image"]] placeholderImage:[UIImage imageNamed:@"10.jpg"]];
                layout.alias.text = [smallDic objectForKey:@"alias"];
                layout.name.text = [NSString stringWithFormat:@"户型:%@", [smallDic objectForKey:@"name"]];
                layout.area.text = [NSString stringWithFormat:@"面积:%@", [smallDic objectForKey:@"area"]];
                layout.orient.text = [NSString stringWithFormat:@"朝向:%@", [smallDic objectForKey:@"orient"]];
                self.collectionButton.enabled = YES;
                return layout;
            }
        }
        if (indexPath.row == 1) {
            UITableViewCell *mycell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
            mycell.selectionStyle = UITableViewCellSelectionStyleNone;
            mycell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            mycell.textLabel.text = @"更多户型";
            mycell.detailTextLabel.text = @">";
            return mycell;
        }
    }
    return nil;
}
//section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
//title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"动态";
    }
    if (section == 1) {
        return @"主力户型";
    }
    return nil;
    
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            return 100;
        }
        return 50;
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return 100;
        }
        return 50;
    }
    return 0;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.dynamicArray count] == 0) {
        NSLog(@"...");
    } else {
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            DynamicJumpViewController *con = [[DynamicJumpViewController alloc] init];
            con.dyArray = self.dynamicArray;
            con.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:con animated:YES];
            [con release];
        }
        if (indexPath.row == 0) {
            DetailMainViewController *det = [[DetailMainViewController alloc] init];
            det.hidesBottomBarWhenPushed = YES;
            det.detail = [self.dynamicArray objectAtIndex:0];
            [self.navigationController pushViewController:det animated:YES];
            [det release];
        }
    }
    if (indexPath.section == 1) {
        LayoutJumpViewController *layout = [[LayoutJumpViewController alloc] init];
        layout.layoutJump = self.layoutArray;
        layout.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:layout animated:YES];
        [layout release];
    }
  }
}
@end
